<template>
  <div class="users-container">
    <div class="users-header">
      <h2>已注册用户列表</h2>
      <div class="header-actions">
        <button @click="refreshUsers" :disabled="isLoading" class="refresh-btn">
          {{ isLoading ? '刷新中...' : '刷新列表' }}
        </button>
        <button @click="handleLogout" class="logout-btn">
          退出登录
        </button>
      </div>
    </div>
    
    <div v-if="currentUser" class="current-user-info">
      <h3>当前登录用户</h3>
      <div class="user-card current">
        <div class="user-avatar">
          {{ currentUser.name?.charAt(0).toUpperCase() || 'U' }}
        </div>
        <div class="user-details">
          <h4>{{ currentUser.name || '未设置用户名' }}</h4>
          <p class="user-email">{{ currentUser.email }}</p>
          <p class="user-meta">
            注册时间: {{ formatDate(currentUser.created) }}
          </p>
        </div>
        <div class="user-badge">当前用户</div>
      </div>
    </div>
    
    <div v-if="error" class="error-message">
      {{ error }}
      <button @click="clearError" class="close-btn">×</button>
    </div>
    
    <div v-if="isLoading && !users.length" class="loading">
      正在加载用户列表...
    </div>
    
    <div v-else-if="users.length === 0" class="empty-state">
      <p>暂无用户数据</p>
    </div>
    
    <div v-else class="users-grid">
      <h3>所有用户 ({{ users.length }} 人)</h3>
      <div class="users-list">
        <div 
          v-for="user in users" 
          :key="user.id" 
          class="user-card"
          :class="{ 'is-current': user.id === currentUser?.id }"
        >
          <div class="user-avatar">
            {{ user.name?.charAt(0).toUpperCase() || 'U' }}
          </div>
          <div class="user-details">
            <h4>{{ user.name || '未设置用户名' }}</h4>
            <p v-if="user.emailVisibility || user.id === currentUser?.id" class="user-email">
              {{ user.email }}
            </p>
            <p v-else class="user-email private">邮箱地址已隐藏</p>
            <p class="user-meta">
              注册时间: {{ formatDate(user.created) }}
            </p>
            <p class="user-meta">
              最后更新: {{ formatDate(user.updated) }}
            </p>
          </div>
          <div v-if="user.id === currentUser?.id" class="user-badge">
            您
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { 
  getAllUsers, 
  logout, 
  isLoading, 
  error, 
  clearError, 
  currentUser 
} from '../stores/auth'
import type { RecordModel } from 'pocketbase'

const router = useRouter()
const users = ref<RecordModel[]>([])

const formatDate = (dateString: string) => {
  return new Date(dateString).toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

const refreshUsers = async () => {
  try {
    const usersList = await getAllUsers()
    users.value = usersList
  } catch (err) {
    console.error('获取用户列表失败:', err)
  }
}

const handleLogout = () => {
  logout()
  router.push('/login')
}

onMounted(() => {
  refreshUsers()
})
</script>

<style scoped>
.users-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
}

.users-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  flex-wrap: wrap;
  gap: 1rem;
}

.users-header h2 {
  color: #333;
  margin: 0;
}

.header-actions {
  display: flex;
  gap: 1rem;
}

.refresh-btn, .logout-btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.9rem;
  transition: background-color 0.2s;
}

.refresh-btn {
  background-color: #007bff;
  color: white;
}

.refresh-btn:hover:not(:disabled) {
  background-color: #0056b3;
}

.refresh-btn:disabled {
  background-color: #6c757d;
  cursor: not-allowed;
}

.logout-btn {
  background-color: #dc3545;
  color: white;
}

.logout-btn:hover {
  background-color: #c82333;
}

.current-user-info {
  margin-bottom: 2rem;
}

.current-user-info h3 {
  color: #333;
  margin-bottom: 1rem;
}

.error-message {
  background-color: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
  border-radius: 4px;
  padding: 1rem;
  margin-bottom: 1rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #721c24;
}

.loading, .empty-state {
  text-align: center;
  padding: 2rem;
  color: #666;
}

.users-grid h3 {
  color: #333;
  margin-bottom: 1rem;
}

.users-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 1rem;
}

.user-card {
  background: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 1.5rem;
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.2s;
  position: relative;
}

.user-card:hover {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
}

.user-card.current {
  border-color: #007bff;
  background-color: #f8f9ff;
}

.user-card.is-current {
  border-color: #28a745;
  background-color: #f8fff9;
}

.user-avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background-color: #007bff;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 1.2rem;
  flex-shrink: 0;
}

.user-details {
  flex: 1;
}

.user-details h4 {
  margin: 0 0 0.5rem 0;
  color: #333;
  font-size: 1.1rem;
}

.user-email {
  margin: 0 0 0.5rem 0;
  color: #666;
  font-size: 0.9rem;
}

.user-email.private {
  font-style: italic;
  color: #999;
}

.user-meta {
  margin: 0.25rem 0;
  color: #888;
  font-size: 0.8rem;
}

.user-badge {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background-color: #28a745;
  color: white;
  padding: 0.25rem 0.5rem;
  border-radius: 12px;
  font-size: 0.7rem;
  font-weight: bold;
}

.user-card.current .user-badge {
  background-color: #007bff;
}

@media (max-width: 768px) {
  .users-container {
    padding: 1rem;
  }
  
  .users-header {
    flex-direction: column;
    align-items: stretch;
  }
  
  .header-actions {
    justify-content: center;
  }
  
  .users-list {
    grid-template-columns: 1fr;
  }
}
</style>
